//
//  XLAllApplyPresenter.m
//  industry
//
//  Created by xl on 2020/11/27.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLAllApplyPresenter.h"
#import "XLAllApplyPresenterProtocol.h"
#import "XLAllApplyViewProtocol.h"
#import "XLAppletModel.h"
#import "XLAppletManager.h"

@interface XLAllApplyPresenter ()<XLAllApplyPresenterProtocol>

@end
@implementation XLAllApplyPresenter

- (void)dealloc {
    XLLog(@"XLAllApplyPresenter dealloc");
}

#pragma mark - XLAllApplyPresenterProtocol
- (void)handleCellData:(id)cellData
                isEdit:(BOOL)isEdit {
    XLAppletModel *model = (XLAppletModel *)cellData;
    if (isEdit) {//编辑状态
        if (model.isCommon) {
            [self requstDeleteAppletId:model];
        } else {
            [self requstAddAppletId:model];
        }
    } else {//前往小程序
        XLAppletManager *manager = [[XLAppletManager alloc] init];
        [manager handleJumpLogic:@(1) url:model.appletUrl appId:model.appId];
    }

}

- (void)requstAddAppletId:(XLAppletModel *)model {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (!ISEmptyNumber(model.appletId)) {
        [params setValue:model.appletId forKey:@"appId"];
    }
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    XLSend(self.baseController.cView, XLAllApplyViewProtocol, showLoadingAnimalWithTitle:@"...");
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypeGET url:XLAddAppletUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
         STRONGSELF
         XLSend(strongSelf.baseController.cView, XLAllApplyViewProtocol, hideLoadingAnimal);
        XLSend(strongSelf.baseController.cView, XLAllApplyViewProtocol, refreshAddApplet:model);
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
         STRONGSELF
         XLSend(strongSelf.baseController.cView, XLAllApplyViewProtocol, hideLoadingAnimal);
    }];
}

- (void)requstDeleteAppletId:(XLAppletModel *)model {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (!ISEmptyNumber(model.appletId)) {
        [params setValue:model.appletId forKey:@"appId"];
    }
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    XLSend(self.baseController.cView, XLAllApplyViewProtocol, showLoadingAnimalWithTitle:@"...");
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypeGET url:XLDeleteAppletUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
         STRONGSELF
         XLSend(strongSelf.baseController.cView, XLAllApplyViewProtocol, hideLoadingAnimal);
        XLSend(strongSelf.baseController.cView, XLAllApplyViewProtocol, refreshDeleteApplet:model);
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
         STRONGSELF
         XLSend(strongSelf.baseController.cView, XLAllApplyViewProtocol, hideLoadingAnimal);
    }];
}

@end
